<?php
namespace App\Services\Main;

use App\Repositories\Main\BaleCostRepository;
use App\Repositories\Main\BaleCostTypeRepository;

class BaleCostService
{
    private $service;
    private $type;

    public function __construct()
    {
        $this->service = new BaleCostRepository();
        $this->type = new BaleCostTypeRepository();
    }

    public function getAdminTypes($baleId)
    {
        return $this->type->getAdminIndex($baleId);
    }

    public function add($data)
    {
        $model = $this->service->getShowByType2($data['type'],$data['name']);
        if ($model) { return ''; }
        return $this->service->setAdd($data);
    }

    public function setModifyName($typeId,$id,$name)
    {
        $model = $this->service->getShowByType2($typeId,$name);
        if ($model) { return ''; }
        return $this->service->setModifyName($id,$name);
    }

    public function setModifyCost($id,$cost)
    {
        return $this->service->setModifyCost($id,$cost);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getTypes($order)
    {

        $models = $this->type->getIndexByOrderId($order);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['orderId'] = $model->order;
            $dataArr[$k]['name'] = $model->name;
            $subModels = $this->service->getShowByType($model->id);
            $subs = array();
            if (count($subModels)) {
                foreach ($subModels as $k2=>$subModel) {
                    $subs[$k2]['subId'] = $subModel->id;
                    $subs[$k2]['subName'] = $subModel->name;
                    $subs[$k2]['subCost'] = $subModel->cost;
                }
            }
            $dataArr[$k]['subs'] = $subs;
        }
        return $dataArr;
    }

    public function setTypeAdd($order,$typeName)
    {
        $model = $this->type->getOneByName($order,$typeName);
        if ($model) { return ''; }
        $dataArr = array(
            'order' => $order,
            'name' => $typeName,
        );
        return $this->type->setAdd($dataArr);
    }

    public function setTypeModify($typeId,$typeName)
    {
        $dataArr = array(
            'name' => $typeName,
        );
        $this->type->setModify($dataArr,$typeId);
        return $typeId;
    }
}